package com.ajhlp.spring_modules;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.scheduling.quartz.JobDetailBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import com.ajhlp.spring_modules.schedule.JobBean;
import com.ajhlp.spring_modules.schedule.TaskBean;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws Exception
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        SchedulerFactoryBean schedulerFactoryBean = context.getBean(SchedulerFactoryBean.class);
        
        TaskBean manualTask = new TaskBean();
        manualTask.setDetail("manual task");
        
        
        Map<String,Object> jobData = new HashMap<String,Object>();
        jobData.put("taskBean", manualTask);
        
        JobDetailBean jobDetail = new JobDetailBean();
        jobDetail.setJobClass(JobBean.class);
        jobDetail.setJobDataAsMap(jobData);
        jobDetail.setName("manualjob");
        jobDetail.setGroup("manualgroup");
        jobDetail.setDescription("manual add this job");
        
        CronTriggerBean manualTrigger = BeanUtils.instantiateClass(CronTriggerBean.class);
        manualTrigger.setJobDetail(jobDetail);
        manualTrigger.setCronExpression("0/10 * * * * ?");
        manualTrigger.setJobName("manualjob");
        manualTrigger.setJobGroup("manualgroup");
        manualTrigger.setName("manualtrigger");
        manualTrigger.setGroup("manualtriggergroup");
        manualTrigger.setDescription("manual job's trigger");
        
       
        schedulerFactoryBean.getScheduler().scheduleJob(jobDetail,manualTrigger);
        
    }
}
